<?php

// @formatter:off
/**
 * @file create.php
 * @author Alejandro Dario Simi
 * @date $Date: 2014-05-04 01:33:52 +0000 (Sun, 04 May 2014) $
 *
 * $Id: create.php 111 2014-05-04 01:33:52Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/frontview/actions/rule/dor/create.php $
 */
// @formatter:on

class ActionControllerRuleDorCreate extends WCActionController {
	//
	// Potected variables.
	/**
	 * @var WCConnection Database pointer shortcut.
	 */
	protected $_db;
	/**
	 * @var string Database tables' prefix shortcut.
	 */
	protected $_dbprefix;
	//
	// Magic methods.
	public function __construct() {
		parent::__construct();
		//
		// Loading shortcuts.
		// @{
		$this->_db = WCConnection::Instance();
		$this->_dbprefix = $this->_db->prefix();
		// @}
	}
	//
	// Public methods.
	public function check() {
		$check = parent::check();

		if($check == WC_HTTPERROR_OK) {
			global $wcProfile;
			global $wcRule;

			if(!$wcProfile->allowedTo(WC_PERM_RULE_EDIT)) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_FORBIDDEN;
				$errno = $check;
				$error = "You don't have enough priviliges";
			}
			if($check == WC_HTTPERROR_OK && (!$wcRule || !$wcRule->ok())) {
				global $errno;
				global $error;

				$check = WC_HTTPERROR_BAD_REQUEST;
				$errno = $check;
				$error = "Invalid rule";
			}
		}

		return $check;
	}
	//
	// Protected methods.
	protected function loadRuleInfo() {
		//
		// Loading global pointers.
		global $wcRule;

		foreach(WCViewInfoFactory::LoadInfo($wcRule) as $key => $value) {
			eval("\$this->_manager->info_rule_{$key} = \$value;");
		}
		foreach(WCViewInfoFactory::LoadInterfacesInfo($wcRule) as $key => $value) {
			eval("\$this->_manager->info_rule_{$key} = \$value;");
		}
	}
	protected function subRun() {
		$ok = true;

		//
		// Loading global pointers.
		global $wcAction;
		global $wcRule;

		$this->_manager->PATH = "Action Create: For rule '{$wcRule->name()}'";
		$this->_manager->form_action = requestParams(true, $wcAction, array(), false);

		$this->loadRuleInfo();

		return $ok;
	}
	protected function subRunPOST() {
		$ok = true;
		//
		// Loading global pointers.
		global $wcRule;
		global $wcRuleAction;

		$this->_manager->status = false;

		$wcRule->indexed(false);

		$stmt = null;
		$stmtId = __CLASS__."::".__FUNCTION__."[1]";
		if($this->_db->has($stmtId)) {
			$stmt = $this->_db->get($stmtId);
		} else {
			$query = "insert\n";
			$query .= "        into {$this->_dbprefix}rule_actions (\n";
			$query .= "                rel_ra_rule, rel_ra_type, rel_ra_name, rel_ra_info)\n";
			$query .= "        values (:rule, :type, :name, :info)\n";

			$stmt = $this->_db->prepare($stmtId, $query);
		}

		$params = array(
			":rule" => $wcRule->id(),
			":type" => WCDirectoryOrganizer::$TYPE,
			":name" => $_POST["name"],
			":info" => serialize(array("mode" => $_POST["mode"]))
		);

		if($stmt->execute($params)) {
			$this->_manager->status = true;
			$id = $this->_db->lastInsertId();
			$wcRule->count++;

			$wcRule->reload();
			foreach($wcRule->actions() as $action) {
				if($action->id() == $id) {
					$wcRuleAction = $action;
					break;
				}
			}
		}

		WCViewInfoFactory::LoadInfo($wcRuleAction, true);

		return $ok;
	}
}
